package com.lej.core.widget.vip

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.core.view.isVisible
import androidx.lifecycle.MutableLiveData
import com.lej.core.R
import com.lej.core.common.VIP3
import com.lej.core.common.VIPLevel
import com.lej.core.dsl.*
import com.lej.core.provider.resourceProvider
import com.lej.core.utils.changeQiniuSize
import com.lej.core.utils.load
import kotlin.math.min

/**vip头像*/
open class VipAvatarView(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr) {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)


    private val vipLevel = MutableLiveData<Int>()
    private val avatar = MutableLiveData<Any>()
    fun setVipLevel(vipLevel: Int) {
        this.vipLevel.value = vipLevel
    }

    fun setAvatar(avatar: String?) {
        this.avatar.value = avatar
    }

    fun setDrawableRes(@DrawableRes drawableId: Int) {
        this.avatar.value = drawableId
    }

    ///设计图头像大小
    open val avatarSize = 67

    ///设计图边框大小
    open val borderSize = 93

    ///设计图新秀vip图大小
    open val vipIconSize = 15

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        val size = min(width, height)

        val newWidthMeasureSpec =
            MeasureSpec.makeMeasureSpec(size, MeasureSpec.getMode(widthMeasureSpec))
        val newHeightMeasureSpec =
            MeasureSpec.makeMeasureSpec(size, MeasureSpec.getMode(heightMeasureSpec))
        setMeasuredDimension(size, size)
        findViewById<View>("iv_avatar".toLayoutId())?.measure(
            newWidthMeasureSpec,
            newHeightMeasureSpec
        )
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        findViewById<View>("iv_vip_border".toLayoutId())?.let {
            val offset = width * (borderSize - avatarSize) / 2 / avatarSize
            it.layout(-offset, -offset, offset + width, offset + height)
        }
        findViewById<View>("iv_vip".toLayoutId())?.let {
            if (!it.isVisible) return
            val imgWidth = width * vipIconSize / avatarSize
            val y = width - imgWidth / 2
            val x = width / 2 - imgWidth / 2
            it.layout(x, y, x + imgWidth, y + imgWidth)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        QMUIRadiusImageView {
            layout_id = "iv_avatar"
            layoutParams = LayoutParams(match_parent, match_parent, gravity_center)

            cornerRadius = 200.dp
            borderWidth = 0
            setImageResource(R.drawable.ic_default_avatar)
            observe(avatar) {
                when (it) {
                    is String ->
                        load(
                            it.changeQiniuSize(avatarSize.dp, avatarSize.dp),
                            defaultImg = R.drawable.ic_default_avatar
                        ) {
                            centerCrop()
                        }
                    is Int -> setImageResource(it)
                    is Drawable -> setImageDrawable(it)
                    is Bitmap -> setImageBitmap(it)
                    else -> setImageResource(R.drawable.ic_default_avatar)
                }
            }
            inEdit {
                setImageResource(R.drawable.ic_default_avatar)
            }
        }
        ImageView {
            layout_id = "iv_vip_border"
            layoutParams = LayoutParams(0, 0, gravity_center)
            observe(vipLevel) {
                setImageResource(getVipAvatarNewBorder(it))
            }

            inEdit {
                setImageResource(getVipAvatarNewBorder(VIP3))
            }
        }
//        ImageView {
//            layout_id = "iv_vip"
//            layoutParams = LayoutParams(wrap_content, wrap_content)
//            isInvisible = true
//
//            setImageResource(R.drawable.ic_vip_normal)
//            observe(vipLevel) {
//                if (it != VIP1) {
//                    isInvisible = true
//                    return@observe
//                }
//                isVisible = true
//            }
//            inEdit {
//                isVisible = true
//            }
//        }
    }

    open fun getVipAvatarNewBorder(@VIPLevel vipLevel: Int) =
        resourceProvider.getVipAvatarNewBorder(vipLevel)
}
